using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
namespace MicroMsg.UI.Page
{
	public class ForwardPage : BasePage
	{
		private const string TAG = "SessionView";
		private string mforwardUserName;
		public static Conversation FORWARD_HEAD_CONVERSION;
		private MMObservableCollection<Conversation, SessionListItem> mDataList = new MMObservableCollection<Conversation, SessionListItem>();
		private List<Conversation> mDelayUpdateConv = new List<Conversation>();
		private EventWatcher mListChangeWatcher;
		private bool isConversationInit;
		internal Grid LayoutRoot;
		internal panelTitle2Line panelTitle;
		internal Grid ContentPanel;
		internal MMListBox SessionList;
		private bool _contentLoaded;
		public ForwardPage()
		{
			if (ForwardPage.FORWARD_HEAD_CONVERSION == null)
			{
				ForwardPage.FORWARD_HEAD_CONVERSION = new Conversation();
				ForwardPage.FORWARD_HEAD_CONVERSION.strUsrName = "$internal_define$___FORWARD_HEAD_CONVERSION";
				ForwardPage.FORWARD_HEAD_CONVERSION.nTime = 9223372036854775807L;
			}
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.SessionView_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.SessionView_Unloaded));
			this.SessionList.IsSessionListBox = true;
			if (ForwardMsgHelper.GetForwardKind() == ForwardKind.Kind_ShareCard)
			{
				this.panelTitle.strSecondTitle = strings.Contacts_Confirm_Title.Trim().ToLower();
				return;
			}
			this.panelTitle.strSecondTitle = strings.ChatItem_Forward.Trim().ToLower();
		}
		~ForwardPage()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (!ForwardMsgHelper.HasMsgToForward())
			{
				base.GoBack();
			}
		}
		private void SessionView_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		public void SessionUnit()
		{
			this.ConversationUninit();
		}
		private void SessionView_Loaded(object sender, RoutedEventArgs e)
		{
			this.ConversationInit();
		}
		private void forwardMessage(string cuUser)
		{
			if (!ForwardMsgHelper.HasMsgToForward())
			{
				return;
			}
			ForwardMsg forwardMsg = ForwardMsgHelper.GetForwardMsg();
			if (forwardMsg.mKind == ForwardKind.Kind_NormalMsg)
			{
				ChatMsg chatMsg = forwardMsg.mObjContent as ChatMsg;
				if (chatMsg == null)
				{
					return;
				}
				if (!chatMsg.isQmessageTxt() && !chatMsg.IsTxt())
				{
					if (chatMsg.IsImage())
					{
						string text = chatMsg.strPath;
						if (string.IsNullOrEmpty(text))
						{
							text = chatMsg.strThumbnail;
						}
						byte[] array = StorageIO.readFromFile(text);
						if (array != null && array.Length > 0)
						{
							MemoryStream origStream = new MemoryStream(array);
							string filename = Guid.NewGuid().ToString();
							if (cuUser.EndsWith("@qqim"))
							{
								ServiceCenter.sceneSendImg.doScene(cuUser, filename, origStream, 39, null);
							}
							else
							{
								ServiceCenter.sceneSendImg.doScene(cuUser, filename, origStream, 3, null);
							}
							ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_MEDIA_TRANSFER, "0" + 3);
							return;
						}
					}
					else
					{
						if (chatMsg.IsCustomSmiley())
						{
							this.sendCustomSmiley(chatMsg, cuUser);
							return;
						}
						if (chatMsg.IsVideo())
						{
							string msgThumbnail = ChatMsgMgr.getMsgThumbnail(chatMsg);
							string strPath = chatMsg.strPath;
							if (!string.IsNullOrEmpty(msgThumbnail) && !string.IsNullOrEmpty(strPath) && forwardMsg.mArgs != null)
							{
								int playLength = (int)forwardMsg.mArgs;
								ServiceCenter.sceneUploadVideo.doSence(cuUser, strPath, msgThumbnail, playLength);
								return;
							}
							return;
						}
					}
				}
				else
				{
					bool flag = false;
					bool flag2 = false;
					string text2 = EmojiManager.GetInstance().ConvertInputStrToCodeStr(chatMsg.strMsg, ref flag);
					text2 = EmojiManager.GetInstance().SbCodeToUtf16Code(text2, ref flag2);
					if (!chatMsg.IsSender() && chatMsg.strTalker.EndsWith("@chatroom") && text2.IndexOf(':') != -1)
					{
						text2 = text2.Substring(text2.IndexOf(':') + 1).Trim();
					}
					ChatMsg chatMsg2;
					if (cuUser.EndsWith("@qqim"))
					{
						chatMsg2 = ServiceCenter.sceneSendMsg.buildChatMsg(cuUser, text2, 36);
					}
					else
					{
						chatMsg2 = ServiceCenter.sceneSendMsg.buildChatMsg(cuUser, text2, 1);
					}
					if (chatMsg2 != null)
					{
						int emojiType = 1;
						if (flag || flag2)
						{
							emojiType = 2;
						}
						ServiceCenter.sceneSendMsg.doSendMsg(chatMsg2, emojiType);
						return;
					}
				}
			}
			else
			{
				if (forwardMsg.mKind == ForwardKind.Kind_ShareCard)
				{
					if (forwardMsg.mObjContent == null)
					{
						return;
					}
					Contact contact = forwardMsg.mObjContent as Contact;
					if (contact == null)
					{
						return;
					}
					string contactCardContent = ServiceCenter.sceneSendMsg.getContactCardContent(contact);
					ChatMsg chatMsgInfo = ServiceCenter.sceneSendMsg.buildChatMsg(cuUser, contactCardContent, 42);
					ServiceCenter.sceneSendMsg.doSendMsg(chatMsgInfo, 1);
				}
			}
		}
		private void sendCustomSmiley(ChatMsg chatMsg_0, string cuUser)
		{
			EmojiDownloadInfo emojiDownloadInfo = EmojiImageService.getEmojiDownloadInfo(chatMsg_0.strMsg);
			if (emojiDownloadInfo == null || string.IsNullOrEmpty(emojiDownloadInfo.strMd5))
			{
				return;
			}
			if (emojiDownloadInfo.nGameExtType > 0)
			{
				CustomSmileyManager.SendGame(cuUser, emojiDownloadInfo.nGameExtType);
				return;
			}
			CustomSmiley customSmiley = CustomSmileyManager.GetCustomSmileyByMd5(emojiDownloadInfo.strMd5);
			if (customSmiley == null)
			{
				customSmiley = new CustomSmiley(CustomSmileyType.CustomAdd, chatMsg_0.strThumbnail, chatMsg_0.strPath, emojiDownloadInfo.strMd5);
			}
			if (!string.IsNullOrEmpty(customSmiley.ThumbSrc) && !string.IsNullOrEmpty(customSmiley.ImageSrc))
			{
				if (chatMsg_0.strPath.StartsWith("/Resource"))
				{
					chatMsg_0.strPath = chatMsg_0.strPath.Substring(1);
				}
				Stream gifStream;
				if (chatMsg_0.strPath.StartsWith("Resource"))
				{
					Uri uri = new Uri(chatMsg_0.strPath, 2);
					StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
					if (resourceStream == null)
					{
						return;
					}
					gifStream = resourceStream.get_Stream();
				}
				else
				{
					byte[] array = StorageIO.readFromFile(chatMsg_0.strPath);
					gifStream = new MemoryStream(array);
				}
				CustomSmileyManager.SendGif(cuUser, gifStream, customSmiley);
				return;
			}
		}
		public void ConversationUpdate(Conversation conversation_0, bool isUpdateFromList = false)
		{
			if (conversation_0 == null)
			{
				return;
			}
			if (!BindHintManager.IsBindHintConv(conversation_0) && !GuideHeadSetMgr.IsSetHeadHintConv(conversation_0) && !ConversationMgr.IsSupport(conversation_0))
			{
				return;
			}
			bool flag = true;
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				Conversation conversation = this.mDataList[i];
				if (conversation.strUsrName == conversation_0.strUsrName)
				{
					this.mDataList.RemoveAt(i);
					IL_6B:
					if (flag)
					{
						int num = this.mDataList.get_Count();
						for (int j = 0; j < this.mDataList.get_Count(); j++)
						{
							Conversation conversation2 = this.mDataList[j];
							if (conversation_0.nTime > conversation2.nTime)
							{
								num = j;
								IL_AF:
								this.mDataList.Insert(num, conversation_0);
								goto IL_BC;
							}
						}
						goto IL_AF;
					}
					IL_BC:
					if (!isUpdateFromList)
					{
						this.SessionList.ForceComputeVisible();
					}
					return;
				}
			}
			goto IL_6B;
		}
		public void DelayUpdateConversation()
		{
			int num = 2147483647;
			int num2 = 0;
			while (num2 < num && num2 < this.mDelayUpdateConv.get_Count())
			{
				this.ConversationUpdate(Enumerable.First<Conversation>(this.mDelayUpdateConv), true);
				this.mDelayUpdateConv.RemoveAt(0);
				num2++;
				num2++;
			}
			if (this.mDelayUpdateConv.get_Count() == 0)
			{
				this.SessionList.ForceComputeVisible();
				return;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				List<UITask> list = new List<UITask>();
				list.Add(new UITask(new UITaskDlg(this.DelayUpdateConversation), 0u));
				UITaskManager uITaskManager = new UITaskManager(list, null);
				uITaskManager.StartTasks(200u);
			});
		}
		public void ConversationUpdate(ObservableCollection<Conversation> cvsList)
		{
			if (cvsList == null)
			{
				return;
			}
			int num = 0;
			cvsList = Enumerable.OrderBy<Conversation, long>(cvsList, (Conversation conversation_0) => conversation_0.nTime).ToObservableCollection<Conversation>();
			for (int i = cvsList.get_Count() - 1; i >= 0; i--)
			{
				Conversation conversation = cvsList.get_Item(i);
				if (num < 6)
				{
					this.ConversationUpdate(conversation, true);
				}
				else
				{
					this.mDelayUpdateConv.Add(conversation);
				}
				num++;
			}
			this.SessionList.ForceComputeVisible();
			base.get_Dispatcher().BeginInvoke(delegate
			{
				List<UITask> list = new List<UITask>();
				list.Add(new UITask(new UITaskDlg(this.DelayUpdateConversation), 0u));
				UITaskManager uITaskManager = new UITaskManager(list, null);
				uITaskManager.StartTasks(100u);
			});
		}
		public void ConversationDelete(string userName)
		{
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				Conversation conversation = this.mDataList[i];
				if (conversation.strUsrName == userName)
				{
					this.mDataList.RemoveAt(i);
					IL_3F:
					this.SessionList.ForceComputeVisible();
					return;
				}
			}
			goto IL_3F;
		}
		private void ConversationInit()
		{
			if (this.isConversationInit)
			{
				return;
			}
			ObservableCollection<Conversation> list = ServiceCenter.conversationMgr.getList();
			ObservableCollection<Conversation> observableCollection = new ObservableCollection<Conversation>();
			observableCollection.Insert(0, ForwardPage.FORWARD_HEAD_CONVERSION);
			if (list != null)
			{
				using (IEnumerator<Conversation> enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Conversation current = enumerator.get_Current();
						if (!(current.strUsrName == "BindHint@BindHint") && !(current.strUsrName == "SetHeadHint@SetHeadHint") && !ToolFunction.systemPluginName.Contains(current.strUsrName))
						{
							observableCollection.Add(current);
						}
					}
				}
			}
			this.ConversationUpdate(observableCollection);
			this.mDataList.Bind(this.SessionList);
			this.isConversationInit = true;
		}
		private void ConversationUninit()
		{
			if (!this.isConversationInit)
			{
				return;
			}
			this.isConversationInit = false;
		}
		private void SessionListItem_ItemClick(object sender, object object_0)
		{
			Conversation conversation = object_0 as Conversation;
			if (conversation == null)
			{
				return;
			}
			if (conversation.strUsrName == ForwardPage.FORWARD_HEAD_CONVERSION.strUsrName)
			{
				this.btnNewSession_Click(sender, null);
				return;
			}
			if (MainPage.mainPageInstance == null)
			{
				return;
			}
			ChatContainerPage.GoToChatPage(this, conversation.strUsrName, null, null, null);
			this.mforwardUserName = conversation.strUsrName;
			this.confirmForward();
		}
		private void btnNewSession_Click(object sender, RoutedEventArgs e)
		{
			List<string> list = new List<string>();
			string originMsgTalker = ForwardMsgHelper.GetOriginMsgTalker();
			if (ContactMgr.getUserType(originMsgTalker) == ContactMgr.UserType.UserTypeChatRoom)
			{
				List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(originMsgTalker);
				if (chatRoomMemberContactList != null && chatRoomMemberContactList.get_Count() >= 1)
				{
					using (List<Contact>.Enumerator enumerator = chatRoomMemberContactList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Contact current = enumerator.get_Current();
							list.Add(current.strUsrName);
						}
						goto IL_88;
					}
				}
				Log.smethod_1("SessionView", "AddChatPeople,currentRoomInfo == null || currentRoomInfo.Count < 1");
				return;
			}
			list.Add(originMsgTalker);
			list.Add(AccountMgr.getCurAccount().strUsrName);
			IL_88:
			base.GoToPage("/Source/UI/Chat/forward/ForwardContactsPage.xaml", new object[]
			{
				new ForwardPeopleComplete(this.ForwardPeopleComplete),
				list,
				originMsgTalker
			}, false);
		}
		private void ForwardPeopleComplete(bool isSuccess, string userName)
		{
			if (!isSuccess)
			{
				return;
			}
			if (userName != null && userName.get_Length() != 0)
			{
				this.mforwardUserName = userName;
				this.confirmForward();
				return;
			}
		}
		private void confirmForward()
		{
			if (ForwardMsgHelper.GetForwardKind() == ForwardKind.Kind_NormalMsg)
			{
				MMMessageBox mMMessageBox = MMMessageBox.Show(strings.ChatForward_Confirm, null, true, strings.COM_Confirm, strings.COM_Cancel);
				mMMessageBox.set_FontSize(22.0);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(this.MsgBoxClearDataHandler);
				return;
			}
			if (ForwardMsgHelper.GetForwardKind() == ForwardKind.Kind_ShareCard)
			{
				MMMessageBox mMMessageBox2 = MMMessageBox.Show(strings.ChatForward_Confirm, null, true, strings.COM_Confirm, strings.COM_Cancel);
				mMMessageBox2.set_FontSize(22.0);
				mMMessageBox2.ReturnEvent = new DlgReturnEvent(this.MsgBoxClearDataHandler);
			}
		}
		public void MsgBoxClearDataHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				if (this.mforwardUserName != null && this.mforwardUserName.get_Length() > 0)
				{
					this.forwardMessage(this.mforwardUserName);
				}
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/forward/ForwardPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.SessionList = (MMListBox)base.FindName("SessionList");
		}
	}
}
